import {
	ref
} from 'vue'
import {
	defineStore
} from 'pinia'
import userApi from '@/api/user.js'
import storage from '@/utils/storage.js'

export const useUserStore = defineStore('user', () => {

	const user = ref({})
	const token = ref("")

	function login(username, password) {
		return new Promise((resolve, reject) => {
			userApi.login({
				username,
				password
			}).then(res => {
				token.value = res.data;
				storage.setStorageToekn(res.data);
				resolve(res);
			});
		}).catch(err => {
			reject(err)
		})
	}

	function getUserInfo(token) {
		return new Promise((resolve, reject) => {
			userApi.getUserInfo(token).then(res => {
				user.value = res.data;
				// storage.setStorageToekn(token);
				resolve(res);
			});
		})
		
	}

	function logout(token) {
		return new Promise((resolve, reject) => {
			userApi.logout(token).then(res => {
				user.value = {};
				token.value = "";
				storage.removeStorageToken();
				resolve(res);
			});
		}).catch(err => {
			reject(err)
		})
		
	}

	return {
		user,
		token,
		login,
		getUserInfo,
		logout
	}
})